﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Vikipedia.CommonTools.Subtitles
{
    public  interface   ISubtitleItem
    {
        int IndexIfAvsilable { get; set; }
        TimeSpan Position{get;set;}
        TimeSpan Duration {get;set;}
        object Data { get; set; }
    }
    public interface ISubtitleItem<T> : ISubtitleItem
    {
         T Data { get; set; }
    }
    public struct SubtitleItem<T> : ISubtitleItem<T>
    {

        #region ISubtitleItem<T> Members

        public T Data
        {
            get;
            set;

        }

        #endregion

        #region ISubtitleItem Members

        public TimeSpan Position
        {
            get;
            set;
        }

        object ISubtitleItem.Data
        {
            get
            {
                return this.Data;
            }
            set
            {
                if (!(value is T)) throw new ArgumentException("Value Must be " + typeof(T).ToString ());
                this.Data = (T)value;
            }
        }
        public int  IndexIfAvsilable
        {
        get;set;
        }
        public TimeSpan Duration
        {
            get;
            set;
        }
}
        #endregion


 

    public abstract   class SubtitleReader
    {
        public SubtitleReader()
        {
        
        }

        public static IDictionary<FileInfo, SubtitleReader> GetSubtitles(IList<FileInfo> sources)
        {
            var q =
                sources.Select(
                f =>
                    new
                    {
                        reader = Readers.Values.Where(r => r.IsFormatMatch(f)).FirstOrDefault(),
                        file = f
                    }
                )
                .Where(x => x.reader != null);
            IDictionary<FileInfo, SubtitleReader> rval = new Dictionary<FileInfo, SubtitleReader>();
            q.ToList().ForEach(x => rval.Add(x.file, x.reader));
            return rval;

            
        }

        static public IDictionary<string, SubtitleReader> Readers 
        {
            get
            {
                var r = new Dictionary<string, SubtitleReader>();
                r.Add("srt", new SrtSubtitleReader());
                return r;
            }
        
        }



        public abstract IEnumerable<ISubtitleItem>  GetSubtitleItems(FileInfo file);
        public abstract bool IsFormatMatch(FileInfo file);
        public abstract string Name{get;}

    }



}
